﻿using Microsoft.International.Converters.PinYinConverter;
using Microsoft.VisualBasic;
using System.Text;

namespace Translationassistant
{
    public partial class TranslationPage : Form
    {
        public TranslationPage()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;//忽略其他线程执行UI的错误
        }

        /// <summary>
        /// 翻译按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonTranslation_Click(object sender, EventArgs e)
        {
            //线程处理翻译，防止界面卡死
            Thread thread = new(() =>
            {
                try
                {
                    if (textBoxTranslationContent.Text.Length > 1)
                    {
                        if (radioButtonYouDaoInterface.Checked)//有道翻译
                        {
                            textBoxTranslationResults.Text = YouDao.Get(textBoxTranslationContent.Text);//结果       
                        }
                        else if (radioButtonBaiDuInterface.Checked) //百度翻译
                        {
                            BaiDu baiDu = new();

                            textBoxTranslationResults.Text = baiDu.Get(textBoxTranslationContent.Text);//结果  
                        }
                        else if (radioButtonGoogleInterface.Checked) //Google翻译
                        {
                            textBoxTranslationResults.Text = GoogleTranslate.Translate(textBoxTranslationContent.Text);//结果  
                            //textBoxTranslationResults.Text = Google.Get(textBoxTranslationContent.Text);//结果  
                        }
                        else if (rbPinYin.Checked)
                        {
                            var pingyins = PinYinConverterHelp.GetTotalPingYin(textBoxTranslationContent.Text);

                            comboBox1.Items.Clear();
                            foreach (var item in pingyins.TotalPingYin)
                            {
                                comboBox1.Items.Add(item);
                            }
                            comboBox1.Text = comboBox1.Items[0].ToString();

                            comboBox1.Visible = false;

                            if (pingyins.TotalPingYin.Count > 1)
                            {
                                comboBox1.Visible = true;
                            }
      
                            textBoxTranslationResults.Text = comboBox1.Text;
                        }
                        //显示所有单词首字母大写
                        textBoxTranslationResultsConvertToUpper.Text = ConvertToUpper(textBoxTranslationResults.Text!).Replace(" ", "").Replace(",", "");
                        //显示首单词首字母小写，其余单词首字母大写
                        textBoxTranslationResultsFirstCharToLower.Text = FirstCharToLower(textBoxTranslationResultsConvertToUpper.Text).Replace(" ", "").Replace(",", "");
                    }
                    else if (textBoxTranslationResults.Text.Length>1)
                    {
                        //显示所有单词首字母大写
                        textBoxTranslationResultsConvertToUpper.Text = ConvertToUpper(textBoxTranslationResults.Text).Replace(" ", "").Replace(",", "");
                        //显示首单词首字母小写，其余单词首字母大写
                        textBoxTranslationResultsFirstCharToLower.Text = FirstCharToLower(textBoxTranslationResultsConvertToUpper.Text);
                    }
                }
                catch (Exception)
                {

                }
            });
            thread.IsBackground = true;//主线程结束，强制结束子线程
            thread.Start();//执行线程
        }

        /// <summary>
        /// 返回所有单词首字改为大写后的结果
        /// </summary>
        /// <param name="str">普通字符串</param>
        /// <returns>所有单词更改为首字母大写</returns>
        public string ConvertToUpper(string str)
        {
            try
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //设置编码支持GBK2312

                return Strings.StrConv(str, VbStrConv.ProperCase, System.Globalization.CultureInfo.CurrentCulture.LCID) ?? "FALL";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        /// <summary>
        /// 首字母小写
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string FirstCharToLower(string input)
        {
            try
            {
                if (String.IsNullOrEmpty(input))
                    return input;
                string str = input.First().ToString().ToLower() + input.Substring(1);

                return str;
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }

        /// <summary>
        /// 复制首字母小写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonTranslationResultsFirstCharToLower_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxTranslationResultsFirstCharToLower.Text != "")
                {
                    Clipboard.SetText(textBoxTranslationResultsFirstCharToLower.Text);
                }
            }
            catch (Exception)
            {

            }

        }

        /// <summary>
        /// 复制首字母大写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonTranslationResultsConvertToUpper_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxTranslationResultsConvertToUpper.Text != "")
                {
                    Clipboard.SetText(textBoxTranslationResultsConvertToUpper.Text);
                }
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 置顶显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxStickyDisplay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxStickyDisplay.Checked)
                {
                    this.TopMost = true; // 置顶显示
                }
                else
                {
                    this.TopMost = false;
                }
            }
            catch (Exception)
            {

            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //显示所有单词首字母大写
            textBoxTranslationResultsConvertToUpper.Text = ConvertToUpper(comboBox1.Text).Replace(" ", "").Replace(",", "");
            //显示首单词首字母小写，其余单词首字母大写
            textBoxTranslationResultsFirstCharToLower.Text = FirstCharToLower(textBoxTranslationResultsConvertToUpper.Text).Replace(" ", "").Replace(",", "");
        }

        private void TranslationPage_Load(object sender, EventArgs e)
        {
            comboBox1.Visible = false;
        }
    }
}
 